package com.usercontrol;

import com.dao.UserDao;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.SessionAware;

import javax.naming.NamingException;
import javax.servlet.http.Cookie;
import java.sql.SQLException;
import java.util.Map;

/**
 * @author :wn
 * @program : 学生信息管理系统
 * @descript : 登录
 * @create :2020-11-13 21:32
 */

public class LoginAction extends ActionSupport implements SessionAware, ApplicationAware {
    private String username;
    private String password;
    private String remember;
    final String rem="rem";

    public String getRemember() {
        return remember;
    }

    public void setRemember(String remember) {
        this.remember = remember;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getusername() {
        return username;
    }

    public void setusername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String login() throws SQLException, NamingException {
        String next = new String();
        applliction.put("username", username);
        boolean rs = new UserDao().queryMethod(username, password);
        if (rs) {
            if (rem.equals(remember)) {
                Cookie cookie = new Cookie(username, password);
                cookie.setMaxAge(3 * 60);
                ServletActionContext.getResponse().addCookie(cookie);
            }
            session.put("filter", username);
            next = "success";
            return next;
        } else {
            String information = "登录失败！！";
            session.put("information", information);
            next = "error";
            return next;
        }

    }


    Map<String, Object> applliction;

    @Override
    public void setApplication(Map<String, Object> map) {
        this.applliction = map;
    }

    Map<String, Object> session;

    @Override
    public void setSession(Map<String, Object> map) {
        this.session = map;
    }

}
